package com.mjlf.shiro.sha;

import org.apache.shiro.codec.Hex;
import org.apache.shiro.crypto.AesCipherService;
import org.junit.Assert;
import org.junit.Test;

import java.security.Key;

/**
 * @ClassName AesUtil
 * @auther mjlf
 * @Date 2019/6/11 10:33
 * @Description TODO
 */
public class AesUtil {
    private static final AesCipherService aesCipherService;
    private static final Key key;
    static {
        aesCipherService = new AesCipherService();
        aesCipherService.setKeySize(128);
        key = aesCipherService.generateNewKey();
    }
    public static String encrypt(String obj){
        String encrptText = aesCipherService.encrypt(obj.getBytes(),key.getEncoded()).toHex();
        return encrptText;
    }

    public static String decrypt(String obj){
        String result = new String(aesCipherService.decrypt(Hex.decode(obj), key.getEncoded()).getBytes());
        return result;
    }

    @Test
    public void test(){
        String str = encrypt("hello");
        String res = decrypt(str);

        System.out.println(str);
        System.out.println(res);
    }
}
